/**
 * Useritle: UserController.java
 *
 * @author wujin
 * @date 2019年12月30日
 * @version 1.0
 */
package com.pm.controller.security;

import com.github.pagehelper.PageInfo;
import com.pm.common.Pages;
import com.pm.common.Response;
import com.pm.common.annotation.CurrentUser;
import com.pm.common.annotation.MyLog;
import com.pm.common.annotation.UserLoginToken;
import com.pm.common.exception.ExpCodeState;
import com.pm.entity.IndexBean;
import com.pm.entity.po.security.User;
import com.pm.service.security.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Useritle: UserController
 *
 * @author wujin
 * @date 2019年12月30日
 */
@Api(tags = "用户管理")
@UserLoginToken
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    @ApiOperation("新增接口")
    @PostMapping("/add")
    @ResponseBody
    @MyLog(value = "新增用户信息")
    public Response<Integer> add(User user) {
        Response<Integer> response = null;
//		MD5Util.encrypt(user);
        // 根据用户名获取当前用户是否存在，如果不存在则新增，如果已经存在，则不让新增
        Integer countNum = userService.countUserNum(user);
        if (countNum > 0) {
            return new Response<>(ExpCodeState.user_already_exists);
        }
        Integer result = userService.add(user);
        if (result > 0) {
            response = new Response<>(ExpCodeState.add_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.add_data_failed);
        }
        return response;
    }

    @ApiOperation("更新接口")
    @PostMapping("/update")
    @ResponseBody
    @MyLog(value = "编辑用户信息")
    public Response<Integer> update(User user) {
        Response<Integer> response = null;
        Integer result = userService.update(user);
        if (result > 0) {
            response = new Response<>(ExpCodeState.update_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.update_data_failed);
        }
        return response;
    }

    @ApiOperation("删除接口")
    @PostMapping("/remove")
    @ResponseBody
    @MyLog(value = "删除用户信息")
    public Response<Integer> remove(User user) {
        Response<Integer> response = null;
        Integer result = userService.remove(user);
        if (result > 0) {
            response = new Response<>(ExpCodeState.remove_data_success, result);
        } else {
            response = new Response<>(ExpCodeState.remove_data_failed);
        }
        return response;
    }

    @ApiOperation("单个对象查询接口")
    @PostMapping("/load")
    @ResponseBody
    public Response<User> load(User user) {
        Response<User> response = null;
        User result = userService.load(user);
        response = new Response<User>(ExpCodeState.query_data_success, result);
        return response;
    }

    @ApiOperation("全部列表查询接口")
    @PostMapping("/getList")
    @ResponseBody
    public Response<List<User>> getList(User user) {
        List<User> result = userService.getList(user);
        Response<List<User>> res = new Response<>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("分页查询接口")
    @PostMapping("/getListByPage/{pageNum}/{pageSize}")
    @ResponseBody
    @MyLog(value = "查询用户信息")
    public Response<PageInfo<User>> getList(User user, Pages page) {
        PageInfo<User> result = userService.getListByPage(user, page);
        Response<PageInfo<User>> res = new Response<PageInfo<User>>(ExpCodeState.query_data_success, result);
        return res;
    }

    @ApiOperation("当前用户信息查询")
    @PostMapping("/loadCurrentUser")
    @ResponseBody
    Response<IndexBean> loadCurrentUser(@CurrentUser User user) {
        IndexBean result = userService.loadUserInfo(user);
        Response<IndexBean> response = new Response<IndexBean>(ExpCodeState.query_data_success, result);
        return response;
    }

}
